﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mendel
{
    public class Word : Data
    {
        internal bool[] runmodes;

        public bool this[int index]
        {
            get
            {
                if (index <= 2 && index >= 0)
                    return runmodes[index];
                else
                    return false;
            }

            set
            {
                if (index <= 2 && index >= 0)
                    runmodes[index] = value;
            }
        }

        // visszatérési érték: true-ha végzett a futással
        public delegate bool BuiltInFunction(Processor proc);

        private BuiltInFunction builtinfunc;

        public int PC { get; set; }

        public List<Item> Instructions { get; private set; }

        // ha beépített
        public Word(BuiltInFunction builtin, bool no=true, bool sk=false, bool co=false)
        {
            builtinfunc = builtin;
            runmodes = new bool[3];
            runmodes[0] = no;
            runmodes[1] = sk;
            runmodes[2] = co;

            Instructions=null;
            PC=-1;
        }

        // ha felhasználói
        public Word(IEnumerable<Item> instr, bool no=true, bool sk=false, bool co=false)
        {
            Instructions = new List<Item>(instr);
            PC = 0;
            runmodes = new bool[3];
            runmodes[0] = no;
            runmodes[1] = sk;
            runmodes[2] = co;

            builtinfunc = null;
        }

        public override DType Type
        {
            get { return DType.Word; }
        }

        // visszatérési érték: true-ha végzett a futással
        public bool Execute(Processor proc)
        {
            if (proc.RunMode == RMode.Compile)
                proc.DataStack.AddLast(Container);

            if (!runmodes[(int)proc.RunMode])
                return true;

            if (builtinfunc != null)
            {
                return builtinfunc(proc);
            }
            else
            {
                if (Instructions != null && PC >= 0 && PC < Instructions.Count)
                {
                    Instructions[PC]=proc.Execute(Instructions[PC]);
                    PC++;
                    if (PC >= Instructions.Count)
                    {
                        PC = 0;
                        return true;
                    }
                    else
                        return false;
                }
                else
                    return true;
            }
        }

        public override Data Clone()
        {
            if (builtinfunc != null)
                return new Word(builtinfunc, runmodes[0], runmodes[1], runmodes[2]);
            else
                return new Word(Instructions, runmodes[0], runmodes[1], runmodes[2]);
        }

        public override Data Convert(DType to)
        {
            switch (to)
            {
                case DType.Integer:
                    return Convert(DType.List).Convert(DType.Integer);
                case DType.Float:
                    return Convert(DType.List).Convert(DType.Float);
                case DType.String:
                    return Convert(DType.List).Convert(DType.String);
                case DType.List:
                    if (builtinfunc == null)
                        return new List(Instructions);
                    else
                        return new List(new Item[1] { Container });
                case DType.Word:
                    return this;
                case DType.Reference:
                    return new Reference(Container);
                default:
                    return this;
            }
        }
    }
}
